package br.ufc.engsoft.smm.banco;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class ConexaoWebService {
	private static final String URL = "http://www.renanbandeira.com.br/smm/smmwsdl.php";
	private static final String OPERATION = "arraydePerguntas";
	private static final String NAMESPACE = "urn:smmwsdl";

	public static ArrayList<PerguntaWSDL> receberPerguntas(int dificuldade){
		SoapObject request = new SoapObject(NAMESPACE, OPERATION);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		request.addProperty("dificuldade", dificuldade);
		envelope.addMapping(NAMESPACE, "pergunta", new PerguntaWSDL().getClass());
		envelope.setOutputSoapObject(request);
		HttpTransportSE httpTransport = new HttpTransportSE(URL);
		httpTransport.debug = true;
		try {
			httpTransport.call("", envelope);
			Vector a = (Vector) envelope.getResponse();
			ArrayList<PerguntaWSDL> perguntas = new ArrayList<PerguntaWSDL>();
			for(int i=0;i<a.size();i++){
				perguntas.add((PerguntaWSDL) a.get(i));
			}
			return perguntas;
		}
		catch (IOException ioex) {
			ioex.printStackTrace();
			return null;
		}
		catch (XmlPullParserException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static ArrayList<JogadorWSDL> recebeRanking(){
		SoapObject request = new SoapObject(NAMESPACE, "recebeRanking");
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.addMapping(NAMESPACE, "jogador", new JogadorWSDL().getClass());
		envelope.setOutputSoapObject(request);
		HttpTransportSE httpTransport = new HttpTransportSE(URL);
		httpTransport.debug = true;
		try {
			httpTransport.call("", envelope);
			Vector a = (Vector) envelope.getResponse();
			ArrayList<JogadorWSDL> ranking = new ArrayList<JogadorWSDL>();
			for(int i=0;i<a.size();i++){
				ranking.add((JogadorWSDL) a.get(i));
			}
			return ranking;
		}
		catch (IOException ioex) {
			ioex.printStackTrace();
			return null;
		}
		catch (XmlPullParserException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static String atualizaRanking(String nome, int pontuacao){
		SoapObject request = new SoapObject(NAMESPACE, "atualizaRanking");
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.addMapping(NAMESPACE, "jogador", new JogadorWSDL().getClass());
		request.addProperty("jogador", new JogadorWSDL(nome, pontuacao));
		envelope.setOutputSoapObject(request);
		HttpTransportSE httpTransport = new HttpTransportSE(URL);
		httpTransport.debug = true;
		try {
			httpTransport.call("", envelope);
			Object a = (Boolean) envelope.getResponse();
			return a.toString();
		}
		catch (IOException ioex) {
			ioex.printStackTrace();
			return "IO";
		}
		catch (XmlPullParserException e) {
			e.printStackTrace();
			return "XML";
		}
	}
	public static int qtdPerguntas(){
		SoapObject request = new SoapObject(NAMESPACE, "qtdPerguntas");
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		HttpTransportSE httpTransport = new HttpTransportSE(URL);
		httpTransport.debug = true;
		try {
			httpTransport.call("", envelope);
			int qtd = (Integer) envelope.getResponse();
			return qtd;
		}
		catch (IOException ioex) {
			ioex.printStackTrace();
			return -1;
		}
		catch (XmlPullParserException e) {
			e.printStackTrace();
			return -2;
		}
	}
}

